<template>
  <div class="dashboard-screen">
    <!-- 页面标题 -->
    <div class="page-header">
      <div class="header-content">
        <div class="left-section">
          <div class="logo"></div>
          <div class="title-group">
            <h1 class="main-title">数据资料中心</h1>
            <p class="subtitle">统一管理产品、项目、演示系统和公司资源</p>
          </div>
        </div>
        <div class="time-section">
          <div class="current-time">{{ currentTime }}</div>
          <div class="status-badge">
            <span class="status-dot"></span>
            <span>系统运行正常</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 功能模块区域 -->
    <div class="section">
      <div class="section-header">
        <h2 class="section-title">
          <span class="title-icon">🚀</span>
          功能模块
        </h2>
      </div>
      <div class="modules-grid">
        <div class="module-card" v-for="module in modules" :key="module.id" @click="module.action">
          <div class="module-header">
            <div class="module-count">{{ module.count }}</div>
          </div>
          <div class="module-content">
            <h3 class="module-title">{{ module.title }}</h3>
            <p class="module-description">{{ module.description }}</p>
            <div class="module-stats" v-if="module.stats">
              <div class="module-stat" v-for="(stat, index) in module.stats" :key="index">
                <span class="stat-value">{{ stat.value }}</span>
                <span class="stat-desc">{{ stat.desc }}</span>
              </div>
            </div>
          </div>
          <div class="module-footer">
            <div class="action-hint">点击进入 →</div>
          </div>
        </div>
      </div>
    </div>

    <div class="section">
      <div class="section-header">
        <h2 class="section-title">
          <span class="title-icon">🖥️</span>
          系统演示
        </h2>
        <div class="section-badge">{{ demos.length }}个系统</div>
      </div>
      <div class="demo-grid">
        <div class="demo-card" v-for="demo in demos" :key="demo.id" @click="openDemo(demo)">
          <div class="demo-icon">
            <i :class="demo.icon || 'ep:setting'" v-if="demo.icon"></i>
            <span v-else>⚙️</span>
          </div>
          <div class="demo-name">{{ demo.name }}</div>
        </div>
      </div>
    </div>

    <!-- 系统运维区域 -->
    <div class="section">
      <div class="section-header">
        <h2 class="section-title">
          <span class="title-icon">⚙️</span>
          系统运维
        </h2>
        <div class="section-badge">{{ operations.length }}个系统</div>
      </div>
      <div class="demo-grid">
        <div
          class="demo-card"
          v-for="operation in operations"
          :key="operation.id"
          @click="openDemo(operation)"
        >
          <div class="demo-icon">
            <i :class="operation.icon || 'ep:setting'" v-if="operation.icon"></i>
            <span v-else>⚙️</span>
          </div>
          <div class="demo-name">{{ operation.name }}</div>
        </div>
      </div>
    </div>

    <!-- 版权信息 -->
    <div class="footer">
      <div class="footer-content">
        <p class="copyright">© 2025 星链天地 版权所有</p>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { DemoApi } from '@/api/agent/manage/demo'
import { DashboardApi } from '@/api/agent/dashboard'
// 删除这两行错误的导入
// import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
// import { faCoffee } from '@fortawesome/free-solid-svg-icons'
defineOptions({ name: 'DashboardScreen' })

const router = useRouter()

// 当前时间
const currentTime = ref('')
let timeInterval: NodeJS.Timeout

// 更新时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 功能模块
const modules = ref([
  {
    id: 1,
    title: '产品管理',
    description: '管理产品资料和技术文档，包含售前、验收、技术等分类',
    icon: 'fas fa-cube',
    count: 0, // 初始化为0，从接口获取
    status: '运行中',
    statusClass: 'online',
    action: () => router.push('/agent/project-manage/product')
  },
  {
    id: 2,
    title: '项目管理',
    description: '项目进度跟踪和状态管理，支持多种项目状态',
    icon: 'fas fa-project-diagram',
    count: 0, // 初始化为0，从接口获取
    status: '运行中',
    statusClass: 'online',
    action: () => router.push('/agent/manage-project')
  },
  {
    id: 3,
    title: '演示系统',
    description: '系统演示和展示，快速访问各种演示环境',
    icon: 'fas fa-tv',
    count: 0, // 初始化为0，从接口获取
    status: '全部在线',
    statusClass: 'online',
    action: () => router.push('/agent/manage_demo')
  },
  {
    id: 6,
    title: '账号管理',
    description: '管理公司在各个平台的账号信息',
    icon: 'fas fa-user-cog',
    count: 0, // 初始化为0，从接口获取
    status: '就绪',
    statusClass: 'online',
    action: () => router.push('/agent/project-manage/account')
  },
  {
    id: 5,
    title: '公司公开墙',
    description: '技术分享、商机信息、意见反馈',
    icon: 'fas fa-comments',
    count: 0, // 初始化为0，从接口获取
    status: '活跃中',
    statusClass: 'active',
    action: () => router.push('/agent/project-manage/wall')
  }
])

// 演示系统列表
const demos = ref([])

// 系统运维列表
const operations = ref([])

// 获取功能模块统计数据
const getModuleStatistics = async () => {
  try {
    const statistics = await DashboardApi.getStatistics()

    // 更新各模块的统计数据
    modules.value.forEach((module) => {
      switch (module.id) {
        case 1: // 产品管理
          module.count = statistics.productCount || 0
          break
        case 2: // 项目管理
          module.count = statistics.projectCount || 0
          break
        case 3: // 演示系统
          module.count = statistics.demoCount || 0
          break
        case 6: // 账号管理
          module.count = statistics.accountCount || 0
          break
        case 5: // 公司公开墙
          module.count = statistics.wallMessageCount || 0
          break
      }
    })
  } catch (error) {
    console.error('获取模块统计数据失败:', error)
  }
}

// 获取演示系统列表
const getDemos = async () => {
  try {
    const res = await DemoApi.getDemoPage({
      category: 1,
      pageNo: 1,
      pageSize: 50
    })
    demos.value = res.list || []
  } catch (error) {
    console.error('获取演示系统列表失败:', error)
  }
}

// 获取系统运维列表
const getOperations = async () => {
  try {
    const res = await DemoApi.getDemoPage({
      category: 2,
      pageNo: 1,
      pageSize: 50
    })
    operations.value = res.list || []
  } catch (error) {
    console.error('获取系统运维列表失败:', error)
  }
}

// 打开演示系统
const openDemo = (demo) => {
  if (demo.url) {
    window.open(demo.url, '_blank')
  }
}

// 获取状态样式
const getStatusClass = (status: string) => {
  const statusMap = {
    在线: 'status-online',
    离线: 'status-offline',
    维护中: 'status-maintenance',
    开发中: 'status-developing',
    测试中: 'status-testing',
    已发布: 'status-published',
    已下线: 'status-offline'
  }
  return statusMap[status] || ''
}

// 初始化数据
onMounted(() => {
  updateTime()
  timeInterval = setInterval(updateTime, 1000)
  getModuleStatistics() // 获取模块统计数据
  getDemos()
  getOperations()
  console.log('智能管控大屏初始化完成')
})

onUnmounted(() => {
  if (timeInterval) {
    clearInterval(timeInterval)
  }
})
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.dashboard-screen {
  font-family: 'Microsoft YaHei', sans-serif;
  background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
  min-height: 100vh;
  max-height: 100vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  color: #fff;
  padding: 20px;
  position: relative;
  overflow-x: hidden;
  overflow-y: auto;
  box-sizing: border-box;
}

.page-header,
.section,
.footer {
  width: 100%;
  max-width: 100%;
  padding: 0;
  margin-bottom: 20px;
}

.page-header {
  margin-bottom: 20px;
  width: 100%;
  position: relative;

  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(
      90deg,
      rgba(26, 42, 108, 0.9),
      rgba(178, 31, 31, 0.7),
      rgba(253, 187, 45, 0.7)
    );
    border-radius: 0;
    padding: 12px 20px;
    border: none;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    width: 100%;
    min-height: 60px;
    margin: 0;

    &:hover {
      box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
    }
  }

  .left-section {
    display: flex;
    align-items: center;
    gap: 15px;
    flex: 1;
  }

  .logo {
    width: 163px;
    height: 60px;
    background-image: url('/星链天地 横向白色logo2.png');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    flex-shrink: 0;
    display: block;
  }

  .title-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
  }

  .main-title {
    font-size: 1.4em;
    margin: 0;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
    color: #ffffff;
    white-space: nowrap;
    font-weight: 600;
  }

  .subtitle {
    font-size: 0.85em;
    margin: 0;
    color: rgba(255, 255, 255, 0.9);
    white-space: nowrap;
  }

  .time-section {
    display: flex;
    align-items: center;
    gap: 15px;
    flex-shrink: 0;
  }

  .current-time {
    font-size: 0.9em;
    white-space: nowrap;
    color: #ffffff;
    font-weight: 500;
  }

  .status-badge {
    display: flex;
    align-items: center;
    gap: 5px;
    white-space: nowrap;
    background-color: rgba(0, 0, 0, 0.2);
    padding: 4px 10px;
    border-radius: 12px;

    .status-dot {
      width: 8px;
      height: 8px;
      background-color: #4ade80;
      border-radius: 50%;
      display: inline-block;
      animation: pulse 2s infinite;
    }

    span {
      font-size: 0.85em;
      color: #ffffff;
    }
  }
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.9);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(74, 222, 128, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(74, 222, 128, 0);
  }
}

.section {
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 0 20px;
  }

  .section-title {
    font-size: 1.5em;
    margin: 0;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 10px;
    color: #fff;

    .title-icon {
      font-size: 0.8em;
    }
  }

  .section-badge {
    background: rgba(255, 255, 255, 0.2);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.8em;
    color: rgba(255, 255, 255, 0.9);
  }
}

.modules-grid,
.demo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
  padding: 0 20px;
  width: 100%;
}

.module-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  padding: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition:
    transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94),
    box-shadow 0.3s ease;
  display: flex;
  flex-direction: column;
  cursor: pointer;

  &:hover {
    transform: translateY(-8px) scale(1.08);
    box-shadow:
      0 20px 50px rgba(255, 107, 107, 0.4),
      0 10px 25px rgba(0, 0, 0, 0.3);
    background: rgba(255, 255, 255, 0.18);
  }

  .module-header {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 15px;
  }

  .module-count {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 1.2em;
    font-weight: 600;
    min-width: 50px;
    text-align: center;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
  }

  .module-content {
    flex: 1;
    text-align: center;
  }

  .module-title {
    font-size: 1.3em;
    color: #fff;
    margin-bottom: 10px;
    font-weight: 600;
  }

  .module-description {
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.9em;
    line-height: 1.4;
    margin-bottom: 15px;
  }

  .module-footer {
    text-align: center;
    padding-top: 15px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);

    .action-hint {
      color: rgba(255, 255, 255, 0.6);
      font-size: 0.9em;
    }
  }
}

.demo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 15px;
  padding: 0 20px;
  width: 100%;
}

.demo-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  padding: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition:
    transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94),
    box-shadow 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  cursor: pointer;
  min-height: 140px;

  &:hover {
    transform: translateY(-12px) scale(1.12);
    box-shadow:
      0 25px 60px rgba(255, 107, 107, 0.5),
      0 15px 30px rgba(0, 0, 0, 0.4);
    background: rgba(255, 255, 255, 0.25);

    .demo-icon {
      transform: rotate(360deg) scale(1.2);
      filter: drop-shadow(0 0 20px rgba(255, 255, 255, 0.8))
        drop-shadow(0 0 40px rgba(255, 107, 107, 0.6));
    }
  }

  .demo-icon {
    font-size: 0.7em;
    margin-bottom: 15px;
    color: #ffffff;
    transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    display: flex;
    align-items: center;
    justify-content: center;

    :deep(.iconify) {
      color: #ffffff !important;
      font-size: 1em;
    }

    :deep(.v-icon) {
      font-size: 1em !important;

      span {
        font-size: 1em !important;
      }
    }

    span {
      font-size: 1em;
      display: block;
    }

    i {
      font-size: 4em;
      color: #ffffff;
    }
  }

  .demo-name {
    font-size: 1.1em;
    font-weight: 600;
    color: #fff;
    margin-bottom: 10px;
    line-height: 1.3;
    word-break: break-word;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  }

  .demo-status {
    font-size: 0.85em;
    padding: 4px 12px;
    border-radius: 12px;
    font-weight: 500;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);

    &.status-online {
      background: rgba(74, 222, 128, 0.3);
      color: #4ade80;
      box-shadow: 0 0 10px rgba(74, 222, 128, 0.3);
    }

    &.status-offline {
      background: rgba(239, 68, 68, 0.3);
      color: #ef4444;
      box-shadow: 0 0 10px rgba(239, 68, 68, 0.3);
    }

    &.status-maintenance {
      background: rgba(251, 191, 36, 0.3);
      color: #fbbf24;
      box-shadow: 0 0 10px rgba(251, 191, 36, 0.3);
    }

    &.status-developing {
      background: rgba(59, 130, 246, 0.3);
      color: #3b82f6;
      box-shadow: 0 0 10px rgba(59, 130, 246, 0.3);
    }

    &.status-testing {
      background: rgba(168, 85, 247, 0.3);
      color: #a855f7;
      box-shadow: 0 0 10px rgba(168, 85, 247, 0.3);
    }

    &.status-published {
      background: rgba(34, 197, 94, 0.3);
      color: #22c55e;
      box-shadow: 0 0 10px rgba(34, 197, 94, 0.3);
    }
  }
}

@media (max-width: 768px) {
  .modules-grid,
  .demo-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .demo-card {
    min-height: 120px;
    padding: 15px;

    .demo-icon {
      font-size: 3em;
      margin-bottom: 12px;
    }

    .demo-name {
      font-size: 1em;
    }
  }
}

@media (max-width: 480px) {
  .modules-grid,
  .demo-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .demo-card {
    min-height: 100px;
    padding: 12px;

    .demo-icon {
      font-size: 2.5em;
      margin-bottom: 10px;
    }

    .demo-name {
      font-size: 0.9em;
      margin-bottom: 8px;
    }

    .demo-status {
      font-size: 0.75em;
    }
  }
}

.footer {
  margin-top: 80px;
  padding: 0 20px;

  .footer-content {
    padding: 10px;
    text-align: center;

    .copyright {
      font-size: 0.9em;
      opacity: 0.8;
      margin-bottom: 10px;
      color: #fff;
    }
  }
}

@media (max-width: 768px) {
  .modules-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .modules-grid {
    grid-template-columns: 1fr;
  }

  .dashboard-screen {
    padding: 10px;
  }

  .section {
    .section-header {
      padding: 0 10px;
    }
  }

  .modules-grid,
  .demo-grid {
    padding: 0 10px;
  }
}
/* 确保body和html也支持滚动 */
:deep(body) {
  overflow: auto;
}

:deep(html) {
  overflow: auto;
}
</style>

